<template>
  <ux-grid
    ref="plxTable"
    max-height="560"
    :highlightCurrentRow="false"
    :widthResize="true"
    :summary-method="getSummaries"
    show-summary
    :cell-style="cellStyle"
  >
    <ux-table-column title="序号" type="index" width="100" />
    <ux-table-column
      :show-overflow-tooltip="true"
      field="batchNumber"
      width="200"
      title="批次号"
    ></ux-table-column>
    <ux-table-column
      :show-overflow-tooltip="true"
      field="materialCode"
      width="200"
      title="设备物资编码"
    ></ux-table-column>
    <ux-table-column
      :show-overflow-tooltip="true"
      field="materialName"
      width="200"
      title="设备物资名称"
    ></ux-table-column>
    <ux-table-column
      :show-overflow-tooltip="true"
      field="materialState"
      width="200"
      title="设备物资说明"
    ></ux-table-column>
    <ux-table-column
      :show-overflow-tooltip="true"
      field="categoryName"
      width="200"
      title="分类名称"
    ></ux-table-column>
    <ux-table-column
      :show-overflow-tooltip="true"
      field="measureUnit"
      width="200"
      title="计量单位"
    ></ux-table-column>
    <ux-table-column
      :show-overflow-tooltip="true"
      field="storageCount"
      width="200"
      title="库存数量"
      :formatter="storageCountFormatter"
    ></ux-table-column>
    <ux-table-column
      :show-overflow-tooltip="true"
      field="unStorageCount"
      width="200"
      title="可用库存数量"
      :formatter="unStorageCountFormatter"
    ></ux-table-column>
    <ux-table-column
      :show-overflow-tooltip="true"
      field="transferLibraryCount"
      width="200"
      title="移库数量"
      :formatter="transferLibraryCountFormatter"
    ></ux-table-column>
    <ux-table-column
      :show-overflow-tooltip="true"
      field="currencyName"
      width="200"
      title="币种"
    ></ux-table-column>
    <ux-table-column
      :show-overflow-tooltip="true"
      field="exchangeRate"
      width="200"
      title="汇率"
    ></ux-table-column>
    <ux-table-column
      :show-overflow-tooltip="true"
      field="unitPriceWithTax"
      width="200"
      title="含税单价"
      :formatter="unitPriceWithTaxFormatter"
    ></ux-table-column>
    <ux-table-column
      :show-overflow-tooltip="true"
      field="moneyWithTax"
      width="200"
      title="含税金额"
      :formatter="moneyWithTaxFormatter"
    ></ux-table-column>
    <ux-table-column
      :show-overflow-tooltip="true"
      field="remark"
      width="200"
      title="备注"
    ></ux-table-column>
  </ux-grid>
</template>

<script>
import BigNumber from "bignumber.js";
import { formatNumber } from "@/basa/utils";

export default {
  props: {
    smpMoveInventoryBill: {
      type: Array
    }
  },
  watch: {
    smpMoveInventoryBill: {
      handler(n) {
        this.$nextTick(() => {
          this.$refs.plxTable.reloadData(n);
        });
      },
      immediate: true
    }
  },
  data() {
    return {
      tableData: []
    };
  },
  mounted() {},
  methods: {
    // 库存数量
    storageCountFormatter({ cellValue, row, column }) {
      return formatNumber(row.storageCount, 3);
    },
    // 可用库存数量
    unStorageCountFormatter({ cellValue, row, column }) {
      return formatNumber(row.unStorageCount, 3);
    },
    // 移库数量
    transferLibraryCountFormatter({ cellValue, row, column }) {
      return formatNumber(row.transferLibraryCount, 3);
    },
    // 含税单价
    unitPriceWithTaxFormatter({ cellValue, row, column }) {
      return Number(row.unitPriceWithTax).toFixed(2);
    },
    // 含税金额
    moneyWithTaxFormatter({ cellValue, row, column }) {
      return Number(row.moneyWithTax).toFixed(2);
    },
    // 合计
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "合计";
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              // return prev + curr;
              return new BigNumber(prev).plus(curr);
            } else {
              return prev;
            }
          }, 0);
          if (
            index == 7 ||
            index == 8 ||
            index == 9 ||
            index == 12 ||
            index == 13
          ) {
            sums[index] = formatNumber(sums[index], 3);
          } else {
            sums[index] = "";
          }
        } else {
          sums[index] = "";
        }
      });

      return [sums];
    },
    cellStyle({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 11) {
        // 指定列号
        return "text-align:right";
      } else {
        return "";
      }
    }
  }
};
</script>

<style></style>
